#####################
#####################

## Replication code for:
## "The Crisis of Identity Politics": 
## Far Right Success and Mainstream Party Attention to Identity Groups"
## Ana Catalano Weeks and Peter Allen

## Analysis carried out using (base) R v 4.1.2

#####################
#####################

rm(list = ls())

#########################
##cluster SE function##
## to cluster by election ##

clx <- 
function(fm, dfcw, cluster){
         library(sandwich);library(lmtest)
         M <- length(unique(cluster))   
         N <- length(cluster)           
         K <- fm$rank                        
         dfc <- (M/(M-1))*((N-1)/(N-K))  
         uj  <- apply(estfun(fm),2, function(x) tapply(x, cluster, sum));
         vcovCL <- dfc*sandwich(fm, meat=crossprod(uj)/N)*dfcw
	return(list(coeftest= coeftest(fm, vcovCL), vcov=vcovCL))
 }
#########################

##################################
#### Function to plot interactions with clustered SEs 
##################################

# meplot()
meplot <- function(model,var1,var2,int,vcov,ci=.95,
                   xlab=var2,ylab=paste("Marginal Effect of",var1),
                   main="Marginal Effect Plot",
                   me_lty=1,me_lwd=1,me_col="black",
                   ci_lty=1,ci_lwd=.5,ci_col="black",
                   yint_lty=2,yint_lwd=1,yint_col="black"){
  require(ggplot2)
  alpha <- 1-ci
  z <- qnorm(1-alpha/2)
  beta.hat <- coef(model)
  cov <- vcov
  z0 <- seq(min(model.frame(model)[,var2],na.rm=T),max(model.frame(model)[,var2],na.rm=T),length.out=1000)
  dy.dx <- beta.hat[var1] + beta.hat[int]*z0
  se.dy.dx <- sqrt(cov[var1,var1] + z0^2*cov[nrow(cov),ncol(cov)] + 2*z0*cov[var1,ncol(cov)])
  upr <- dy.dx + z*se.dy.dx
  lwr <- dy.dx - z*se.dy.dx
  ggplot(data=NULL,aes(x=z0, y=dy.dx)) +
    labs(x=xlab,y=ylab,title=main) +
    geom_line(aes(z0, dy.dx),size = me_lwd, 
              linetype = me_lty, 
              color = me_col) +
    geom_line(aes(z0, lwr), size = ci_lwd, 
              linetype = ci_lty, 
              color = ci_col) +
    geom_line(aes(z0, upr), size = ci_lwd, 
              linetype = ci_lty, 
              color = ci_col) +
    geom_hline(yintercept=0,linetype=yint_lty,
               size=yint_lwd,
               color=yint_col)
}

## Source: Miles Williams, https://rpubs.com/milesdwilliams15/381372

###########################

library(interplot)
library(ggplot2)
library("ggthemes")
library("scales")

## load data 
load("data_march2022.RData")

nrow(newdat)
## 459 observations

######################################
########## For summary statistics ####
########## Table A2 ##################

table(newdat$year)
table(newdat$countryname)
##how many parties? 
table(newdat$party)
unique(newdat$party)
##89
library(stargazer)
stargazer(newdat)

#########################
## Replicates Figure 1 ##
#########################

library(ggplot2)
library(scales)
library(readr)

figures_data <- read_csv("figures_data.csv")
View(figures_data)
nrow(figures_data) ##844

CMPdata<-figures_data

CMPdata$parfam <- as.factor(CMPdata$parfam)

set.seed(1234)

#### identity groups 

CMPdata$idc<-CMPdata$per705 + CMPdata$per706

CMPdata$newpartyfam<-NA
CMPdata$newpartyfam[CMPdata$parfam==30]<-"Social Democrat"
CMPdata$newpartyfam[CMPdata$parfam==40]<-"Liberal"
CMPdata$newpartyfam[CMPdata$parfam==50]<-"Christian Democrat"
CMPdata$newpartyfam[CMPdata$parfam==60]<-"Conservative"
CMPdata$newpartyfam[CMPdata$parfam==77]<-"Far Right"

CMPdata$newpartyfam <- factor(CMPdata$newpartyfam, levels = c("Social Democrat", "Liberal", "Christian Democrat",
"Conservative", "Far Right"))


bypar <- ggplot(data = CMPdata, aes(x = year, y = idc, color = newpartyfam, linetype=newpartyfam )) +
  geom_point(size = 0.1) + geom_smooth(method = "loess", se = FALSE, size=1.8) +
  labs(x="Election Year",
       y="Positive Mentions (% Manifesto)",
       title="Positive Mentions of Non-Economic \nIdentity Groups 1980-2017")

bypar2<- bypar  + theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+
  scale_color_grey(name = "Party family") +
  scale_linetype_discrete(name = "Party family") +
  theme(legend.key.height= unit(.8, 'cm'),
        legend.key.width= unit(1.5, 'cm'))+
theme(text = element_text(size=20)) + coord_cartesian(ylim = c(0, 10))

bypar2
##ggsave("Figure_1a.pdf", width = 10, height = 5)


# working class 

byparb <- ggplot(data = CMPdata, aes(x = year, y = per701, color = newpartyfam, linetype=newpartyfam )) +
  geom_point(size = 0.1) + geom_smooth(method = "loess", se = FALSE, size=1.8) +
  labs(x="Election Year",
       y="Positive Mentions (% Manifesto)",
       title="Positive Mentions of Labour Groups and \nWorking Class 1980-2017")

byparb2<- byparb  + theme_bw() + theme(panel.border = element_blank(), panel.grid.major = element_blank(),
panel.grid.minor = element_blank(), axis.line = element_line(colour = "black"))+
  scale_color_grey(name = "Party family") +
  scale_linetype_discrete(name = "Party family") +
  theme(legend.key.height= unit(.8, 'cm'),
        legend.key.width= unit(1.5, 'cm'))+
theme(text = element_text(size=20)) + coord_cartesian(ylim = c(0, 10))

byparb2
##ggsave("Figure_1b.pdf", width = 10, height = 5)


#########################
### Replicates Table 1 
#########################

## what is the mean and sd of far right growth?
mean(newdat$lag1.prr_vote_new)
sd(newdat$lag1.prr_vote_new)


fm.2b<-lm(idc ~ lag1.prr_vote_new + previous_cabinet_party  + lag1.vote_share2 + lag1.gdp_growth + lag1.unemp + lag1.log_asy +
lag1.exc_row_idc_sal + factor(party) + factor(year), data=newdat)
summary(fm.2b) 
nobs(fm.2b)
C1<- clx(fm.2b, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))
r.est1<-cbind(estimate=fm.2b$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.2b$coefficients/std.err))),5), 
       lower=fm.2b$coefficients-1.96*std.err, 
       upper=fm.2b$coefficients+1.96*std.err)  
r.est1 

fm.3<-lm(per701 ~ lag1.prr_vote_new + previous_cabinet_party  + lag1.vote_share2 + lag1.gdp_growth + lag1.unemp + lag1.log_asy + 
lag1.exc_row_701_sal + factor(party) + factor(year) , data=newdat)
summary(fm.3) 
nobs(fm.3)
C1<- clx(fm.3, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))

r.est2<-cbind(estimate=fm.3$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.3$coefficients/std.err))),5), 
       lower=fm.3$coefficients-1.96*std.err, 
       upper=fm.3$coefficients+1.96*std.err)
       
r.est2

#########################
##### Replicates Table A3 and Figure 2
#########################

## create factor variable for social democratic party 
newdat$Social_democratic_party<-as.factor(newdat$socialdem)

library(margins)
library(marginaleffects)

fm.14bz<-lm(idc ~ lag1.prr_vote_new + lag1.prr_vote_new:Social_democratic_party + 
previous_cabinet_party  + lag1.vote_share2  + lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_idc_sal + factor(party) + factor(year), data=newdat)
summary(fm.14bz) 
C1<- clx(fm.14bz, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))
r.est14bz<-cbind(estimate=fm.14bz$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.14bz$coefficients/std.err))),5), 
       lower=fm.14bz$coefficients-1.96*std.err, 
       upper=fm.14bz$coefficients+1.96*std.err)
       
r.est14bz 

## Note: Figures from cplot take a long time to be produced

cplot(fm.14bz, vcov = C1$vcov, x = "Social_democratic_party", dx = "lag1.prr_vote_new", what = "effect", 
      data = newdat, 
xlab = "Social democratic party",
ylab =  "Marginal Effect of Far Right on Identity Groups",
      main = "")

## Note: add in line at 0 manually 

fm.15<-lm(per701~ lag1.prr_vote_new + lag1.prr_vote_new:Social_democratic_party + previous_cabinet_party  + lag1.vote_share2  + lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_701_sal + factor(party) + factor(year) , data=newdat)
summary(fm.15)
C1<- clx(fm.15, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))
r.est15<-cbind(estimate=fm.15$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.15$coefficients/std.err))),5), 
       lower=fm.15$coefficients-1.96*std.err, 
       upper=fm.15$coefficients+1.96*std.err)
       
r.est15 #

library(margins)
cplot(fm.15, vcov = C1$vcov, x = "Social_democratic_party", dx = "lag1.prr_vote_new", what = "effect", 
      data = newdat, 
xlab = "Social democratic party",
ylab =  "Marginal Effect of Far Right on Working Class",
      main = "")

## note: add in line at 0 manually 

############################
############################
###### Replicates Table A4 and Figure 3
##############################

fm.2e<-lm(idc ~lag1.prr_vote_new + fd.vote_share + lag1.prr_vote_new:fd.vote_share + previous_cabinet_party 
+ lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_idc_sal + factor(party) + factor(year), data=newdat)
summary(fm.2e) 

C1<- clx(fm.2e, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))
r.est2e<-cbind(estimate=fm.2e$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.2e$coefficients/std.err))),5), 
       lower=fm.2e$coefficients-1.96*std.err, 
       upper=fm.2e$coefficients+1.96*std.err)
       
r.est2e

cplot(fm.2e, vcov = C1$vcov, x = "fd.vote_share", dx = "lag1.prr_vote_new", what = "effect", 
      data = newdat, 
xlab = "Vote Change",
ylab =  "Marginal Effect of Far Right on Identity Groups",
      main = "")
## note: add in line at 0 manually 

fm.3d<-lm(per701 ~ lag1.prr_vote_new + fd.vote_share + lag1.prr_vote_new:fd.vote_share + previous_cabinet_party 
+ lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_701_sal + factor(party) + factor(year), data=newdat)
summary(fm.3d) 
nobs(fm.3d)
C1<- clx(fm.3d, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))
r.est3d<-cbind(estimate=fm.3d$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.3d$coefficients/std.err))),5), 
       lower=fm.3d$coefficients-1.96*std.err, 
       upper=fm.3d$coefficients+1.96*std.err)
       
r.est3d

## when does it become significant? vote change = 9
library(dplyr)
x<- margins(fm.3d, vcov = C1$vcov, variables = 'lag1.prr_vote_new',
        at = list(fd.vote_share = -30:20))%>% summary()
x

cplot(fm.3d, vcov = C1$vcov, x = "fd.vote_share", dx = "lag1.prr_vote_new", what = "effect", 
      data = newdat, 
xlab = "Vote Change",
ylab =  "Marginal Effect of Far Right on Working Class",
      main = "")
## note: add in line at 0 manually 

####################################
####################################
############ Robustness checks for Appendix 

####################################
## Replicates Table A5 and Figure A1
####################################

fm.2<-lm(idc ~ factor(party) + factor(year) + lag1.prr_vote_new + lag1.prr_wc + lag1.prr_vote_new:lag1.prr_wc + previous_cabinet_party  + 
lag1.vote_share2 + lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_idc_sal, data=newdat)
summary(fm.2) 
nobs(fm.2)
C1<- clx(fm.2, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))
r.est1<-cbind(estimate=fm.2$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.2$coefficients/std.err))),5), 
       lower=fm.2$coefficients-1.96*std.err, 
       upper=fm.2$coefficients+1.96*std.err)  
r.est1 

p2<-meplot(model=fm.2,var1="lag1.prr_vote_new",var2="lag1.prr_wc",int="lag1.prr_vote_new:lag1.prr_wc",vcov=C1$vcov, ylab="Estimated Coefficient for Identity Groups", 
xlab="Far Right Attn. to Working Class", main="") + 
   theme_classic() + theme(legend.position="none") + theme(text = element_text(size=18)) 
p2


fm.3<-lm(per701 ~ factor(party) + factor(year) + lag1.prr_vote_new + lag1.prr_wc + lag1.prr_vote_new:lag1.prr_wc + previous_cabinet_party  + 
lag1.vote_share2 + lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_701_sal, data=newdat)
summary(fm.3) 
nobs(fm.3)
C1<- clx(fm.3, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))

r.est2<-cbind(estimate=fm.3$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.3$coefficients/std.err))),5), 
       lower=fm.3$coefficients-1.96*std.err, 
       upper=fm.3$coefficients+1.96*std.err)
       
r.est2 

p3<-meplot(model=fm.3,var1="lag1.prr_vote_new",var2="lag1.prr_wc",int="lag1.prr_vote_new:lag1.prr_wc",vcov=C1$vcov, ylab="Estimated Coefficient for Working Class", 
xlab="Far Right Attn. to Working Class", main="") + 
   theme_classic() + theme(legend.position="none") + theme(text = element_text(size=18)) 
p3


###########################
## Replicates Table A6
###########################

fm.3h<-lm(idc ~ factor(party) + lag1.idc  + lag1.prr_vote_new + previous_cabinet_party  + lag1.vote_share2 + 
lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_idc_sal, data=newdat)
summary(fm.3h) 

C1<- clx(fm.3h, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))

r.est3h<-cbind(estimate=fm.3h$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.3h$coefficients/std.err))),5), 
       lower=fm.3h$coefficients-1.96*std.err, 
       upper=fm.3h$coefficients+1.96*std.err)
       
r.est3h

fm.3g<-lm(per701 ~ factor(party) + lag1.per701  + lag1.prr_vote_new + previous_cabinet_party  + lag1.vote_share2  + 
lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_701_sal, data=newdat)
summary(fm.3g) 

C1<- clx(fm.3g, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))

r.est3g<-cbind(estimate=fm.3g$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.3g$coefficients/std.err))),5), 
       lower=fm.3g$coefficients-1.96*std.err, 
       upper=fm.3g$coefficients+1.96*std.err)
       
r.est3g

###########################
## Replicates Table A7
###########################

fm.2<-lm(idc ~ factor(party) + factor(year) + lag1.prr_vote_new , data=newdat)
summary(fm.2) 
nobs(fm.2)
C1<- clx(fm.2, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))
r.est4g<-cbind(estimate=fm.2$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.2$coefficients/std.err))),5), 
       lower=fm.2$coefficients-1.96*std.err, 
       upper=fm.2$coefficients+1.96*std.err)  
r.est4g 


fm.3<-lm(per701 ~ factor(party) + factor(year) + lag1.prr_vote_new , data=newdat)
summary(fm.3) 
nobs(fm.3)
C1<- clx(fm.3, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))

r.est4e<-cbind(estimate=fm.3$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.3$coefficients/std.err))),5), 
       lower=fm.3$coefficients-1.96*std.err, 
       upper=fm.3$coefficients+1.96*std.err)
       
r.est4e

###########################
## Replicates Table A8
###########################

newdat$WE2<-as.factor(newdat$WE)

fm.2<-lm(idc ~ factor(party) + factor(year) + lag1.prr_vote_new + lag1.prr_vote_new:WE2 + previous_cabinet_party  + 
lag1.vote_share2  + lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_idc_sal, data=newdat)
summary(fm.2) 
nobs(fm.2)
C1<- clx(fm.2, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))
r.est5e<-cbind(estimate=fm.2$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.2$coefficients/std.err))),5), 
       lower=fm.2$coefficients-1.96*std.err, 
       upper=fm.2$coefficients+1.96*std.err)  
r.est5e 

interplot(m = fm.2, var1 = "lag1.prr_vote_new", var2 = "WE2" )+
    ylab("Marginal effect of far right on identity groups") + xlab("Western Europe") +
 ggtitle("") +
   theme_classic() + theme(legend.position="none") + theme(text = element_text(size=16)) +
    geom_hline(yintercept = 0, linetype = "dashed") + geom_point(size=3) 

fm.3<-lm(per701 ~ factor(party) + factor(year) + lag1.prr_vote_new + lag1.prr_vote_new:WE2 + previous_cabinet_party  + 
lag1.vote_share2  + lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_701_sal, data=newdat)
summary(fm.3) 
nobs(fm.3)
C1<- clx(fm.3, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))

r.est5f<-cbind(estimate=fm.3$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.3$coefficients/std.err))),5), 
       lower=fm.3$coefficients-1.96*std.err, 
       upper=fm.3$coefficients+1.96*std.err)
       
r.est5f 

library(interplot)

interplot(m = fm.3, var1 = "lag1.prr_vote_new", var2 = "WE2" )+
    ylab("Marginal effect of far right on working class") + xlab("Western Europe") +
 ggtitle("") +
   theme_classic() + theme(legend.position="none") + theme(text = element_text(size=16)) +
    geom_hline(yintercept = 0, linetype = "dashed") + geom_point(size=3) 


############################################
##### Replicates Table A8

newdat$log_wc1<- log( (newdat$per701 +.5) / (newdat$per702 +.5)) ## 

fm.3<-lm(log_wc1 ~ factor(party) + factor(year) + lag1.prr_vote_new + previous_cabinet_party  + lag1.vote_share2 + 
lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_701_sal, data=newdat)
summary(fm.3) 
nobs(fm.3)
C1<- clx(fm.3, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))

r.est2f<-cbind(estimate=fm.3$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.3$coefficients/std.err))),5), 
       lower=fm.3$coefficients-1.96*std.err, 
       upper=fm.3$coefficients+1.96*std.err)
       
r.est2f ###

#####################################################
######## Replicates Table A10
####################################################

## how does the new variable compare to the original ? 

mean(newdat$lag1.prr_vote_largest)
mean(newdat$lag1.prr_vote_new)

### Table 1

fm.2b<-lm(idc ~ lag1.prr_vote_largest + previous_cabinet_party  + lag1.vote_share2 + lag1.gdp_growth + lag1.unemp + lag1.log_asy +
lag1.exc_row_idc_sal + factor(party) + factor(year), data=newdat)
summary(fm.2b) 
nobs(fm.2b)
C1<- clx(fm.2b, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))
r.est1<-cbind(estimate=fm.2b$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.2b$coefficients/std.err))),5), 
       lower=fm.2b$coefficients-1.96*std.err, 
       upper=fm.2b$coefficients+1.96*std.err)  
r.est1 ### the size of effect is larger 

fm.3<-lm(per701 ~ lag1.prr_vote_largest + previous_cabinet_party  + lag1.vote_share2 + lag1.gdp_growth + lag1.unemp + lag1.log_asy + 
lag1.exc_row_701_sal + factor(party) + factor(year) , data=newdat)
summary(fm.3) 
nobs(fm.3)
C1<- clx(fm.3, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))

r.est2<-cbind(estimate=fm.3$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.3$coefficients/std.err))),5), 
       lower=fm.3$coefficients-1.96*std.err, 
       upper=fm.3$coefficients+1.96*std.err)
       
r.est2  

###############################
####### Replicates Table A11 ##
###############################

fm.14b<-lm(idc ~ lag1.prr_vote_largest + lag1.prr_vote_largest:socialdem + previous_cabinet_party  + lag1.vote_share2  + lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_idc_sal + factor(party) + factor(year), data=newdat)
summary(fm.14b) 
C1<- clx(fm.14b, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))
r.est14b<-cbind(estimate=fm.14b$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.14b$coefficients/std.err))),5), 
       lower=fm.14b$coefficients-1.96*std.err, 
       upper=fm.14b$coefficients+1.96*std.err)
       
r.est14b 


###############################
####### Replicates Table A12 ##
###############################

fm.2e<-lm(idc ~lag1.prr_vote_largest + fd.vote_share + lag1.prr_vote_largest:fd.vote_share + previous_cabinet_party 
+ lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_idc_sal + factor(party) + factor(year), data=newdat)
summary(fm.2e) 

C1<- clx(fm.2e, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))
r.est2e<-cbind(estimate=fm.2e$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.2e$coefficients/std.err))),5), 
       lower=fm.2e$coefficients-1.96*std.err, 
       upper=fm.2e$coefficients+1.96*std.err)
       
r.est2e

p5e<-meplot(model=fm.2e,var1="lag1.prr_vote_largest",var2="fd.vote_share",int="lag1.prr_vote_largest:fd.vote_share",vcov=C1$vcov, ylab="Marginal Effect of Far Right on Identity Groups", 
xlab="Vote Change", main="") + 
   theme_classic() + theme(legend.position="none") + theme(text = element_text(size=18)) 
p5e


fm.3d<-lm(per701 ~ lag1.prr_vote_largest + fd.vote_share + lag1.prr_vote_largest:fd.vote_share + previous_cabinet_party 
+ lag1.gdp_growth + lag1.unemp + lag1.log_asy + lag1.exc_row_701_sal + factor(party) + factor(year), data=newdat)
summary(fm.3d) 
nobs(fm.3d)
C1<- clx(fm.3d, 1, newdat$edate)
std.err<-sqrt(diag(C1$vcov))
r.est3d<-cbind(estimate=fm.3d$coefficients, std.err, 
       pvalue=round(2*(1-pnorm(abs(fm.3d$coefficients/std.err))),5), 
       lower=fm.3d$coefficients-1.96*std.err, 
       upper=fm.3d$coefficients+1.96*std.err)
       
r.est3d

## when does it become significant? vote change = 6

margins(fm.3d, vcov = C1$vcov, variables = 'lag1.prr_vote_largest',
        at = list(fd.vote_share = -30:20))%>% summary()

p6<-meplot(model=fm.3d,var1="lag1.prr_vote_largest",var2="fd.vote_share",int="lag1.prr_vote_largest:fd.vote_share",vcov=C1$vcov, 
ylab="Marginal Effect of Far Right on Working Class", 
xlab="Vote Change", main="") + 
   theme_classic() + theme(legend.position="none") + theme(text = element_text(size=18)) 
p6


############################
############################
## matching for case selection ##
############################
############################
mydata = read.csv("Data_for_matching_draft.csv") 
nrow(mydata)

### New matching procedure with confounders used in main analysis 
mydata$log_asy<-log(mydata$no_asy)
head(mydata)

##subset to Denmark + potential matches
mydata<-mydata[ which(mydata$include==1),]
nrow(mydata) ##there are 9 in total

vars<-c("countryname", "year", "avg3_id", "log_asy", "yrs_dem", "gdp_growth", "ilo_unemp", "gdp_pc")
newdata <- mydata[vars]
head(newdata)

##create indicator for country to match (Denmark); all other countries are 0

newdata$ind<-0
newdata$ind[newdata$country=="Denmark"]<-1

## set seed and match 
 library(MatchIt)
set.seed(12345)
nearest.match<-matchit(formula=ind ~ ilo_unemp + log_asy + gdp_pc
 + gdp_growth, data=newdata, method="nearest", distance="mahalanobis")
nearest.match$match.matrix
newdata
##

#  1   
#1 "10"
### matches to Sweden 


